function uuid() {
	var s = [];
	var hexDigits = "0123456789abcdef";
	for (var i = 0; i < 36; i++) {
		s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
	}
	s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
	s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
	s[8] = s[13] = s[18] = s[23] = "-";
	var uuid = s.join("");
	return uuid;
}

/**
 * 加载Gltf
 */
class Tileset {
	constructor(option) {
		this.option = option;
		this.tileset;
		this.load(option);
	}
	load(option) {
		var _this = this;
		var tileset = option.viewer.scene.primitives.add(new Cesium.Cesium3DTileset({
			id: option.id ? option.id : uuid(),
			url: option.url,
			maximumScreenSpaceError: 2,
			maximumNumberOfLoadedTiles: 1000,
			modelMatrix: Cesium.Matrix4.IDENTITY
		}));
		tileset.readyPromise.then(function(tileset) {
			_this.tileset = tileset;
			var boundingSphere = tileset.boundingSphere;
			option.viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0.0, -0.5, boundingSphere.radius *
				2));
			option.viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
			if (option.callback) {
				option.callback(_this);
			}
		}).otherwise(function(error) {
			console.log(error);
		});
	}
	update3dtilesMaxtrix(params) {
		var _this = this;
		//旋转
		var mx = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(params.rx));
		var my = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(params.ry));
		var mz = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(params.rz));
		var rotationX = Cesium.Matrix4.fromRotationTranslation(mx);
		var rotationY = Cesium.Matrix4.fromRotationTranslation(my);
		var rotationZ = Cesium.Matrix4.fromRotationTranslation(mz);
		//平移
		var position = Cesium.Cartesian3.fromDegrees(params.tx, params.ty, params.tz);
		var m = Cesium.Transforms.eastNorthUpToFixedFrame(position);
		//旋转、平移矩阵相乘
		Cesium.Matrix4.multiply(m, rotationX, m);
		Cesium.Matrix4.multiply(m, rotationY, m);
		Cesium.Matrix4.multiply(m, rotationZ, m);
		//赋值给tileset
		_this.tileset._root.transform = m;
	}
}

class Gltf {
	constructor(option) {
		this.option = option;
		this.gltfModel = this.load(option);
	}
	load(option) {
		var _this = this;
		var gltfModel = option.scene.primitives.add(Cesium.Model.fromGltf({
			url: option.url,
			modelMatrix: Cesium.Transforms.eastNorthUpToFixedFrame(Cesium.Cartesian3.fromDegrees(option.lng * 1, option.lat *
				1, option.height * 1)),
			name: option.name ? option.name : '',
			id: option.id ? option.id : uuid(),
			minimumPixelSize: option.minimumPixelSize ? option.minimumPixelSize * 1 : 1,
			maximumScale: option.maximumScale ? option.minimumPixelSize * 1 : 100,
			scale: option.scale ? option.minimumPixelSize * 1 : 1.0
		}));

		if (option.callback) {
			option.callback(_this);
		}
		return gltfModel;
	}
	setHeight(height) {
		var _this = this;
		var height = Number(height);
		if (isNaN(height)) {
			return;
		}
		var cartographic = Cesium.Cartographic.fromCartesian(_this.gltfModel.boundingSphere.center);
		var surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, 0.0);
		var offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, height);
		var translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3());
		_this.gltfModel.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
	}
}
